﻿
using ChestPainCenterSystem.Areas.ChestPainCenter.Models;
using ChestPainCenterSystem.Bll;
using ChestPainCenterSystem.Code.Bll;
using ChestPainCenterSystem.Core;
using ChestPainCenterSystem.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ChestPainCenterSystem.Areas.ChestPainCenter.Controllers
{
    public class PathwayListController : BaseController
    {
        //
        // GET: /ChestPainCenter/PathwayList/

        public ActionResult Index()
        {
            return View();
        }

        public ContentResult GetDiagnosticPathById()
        {
            int id = WebUtils.GetInt("id");
            OperateResult result = PathwayManager.GetDiagnosticPathById(id);
            return JsonDate(result);
        }

        /// <summary>
        /// 加载诊疗路径列表
        /// </summary>
        /// <returns></returns>
        public ContentResult Search()
        {
            //Name=s&page=1&pagesize=30
            string pathName = WebUtils.GetString("Name");
            int pageIndex = WebUtils.GetInt("page", 1);
            int pageSize = WebUtils.GetInt("pagesize", 30);
            OperateResult resultInfo = ZhenLiaoLuJing.GetzhenliaolujingList(0, pageIndex, pageSize, pathName);
            string NoDate = "{Rows:[],Total:0}";
            if (resultInfo.Result == 1)
            {
                var listData = JsonConvert.DeserializeObject<dynamic>(resultInfo.Body.ToString());
                return JsonDate(listData);
            }
            return Content(NoDate);
        }

        /// <summary>
        /// 添加/编辑路径
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit()
        {
            int id = WebUtils.GetInt("id");
            if (id.Equals(0))
            {
                //新增 赋空
                DataTable dt = new DataTable();
                dt.Columns.Add("enable");
                dt.Columns.Add("guanlianzhenliao");
                dt.Columns.Add("create_time");
                dt.Columns.Add("caozuozhe");
                dt.Columns.Add("Id");
                dt.Columns.Add("IsDelete");
                dt.Columns.Add("name");
                dt.Columns.Add("sort");
                dt.Columns.Add("code");

                DataRow dr = dt.NewRow();
                dr["enable"] = false;
                dr["guanlianzhenliao"] = "";
                dr["create_time"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                dr["caozuozhe"] = loginUser.Name;
                dr["Id"] = 0;
                dr["IsDelete"] = 0;
                dr["name"] = "";
                dr["sort"] = 0;
                dr["code"] = "";
                dt.Rows.Add(dr);
                ViewBag.Data = dt;
                return View();
            }
            else
            {
                //编辑 取数据赋值
                OperateResult result = ZhenLiaoLuJing.GetzhenliaolujingList(id, 1, 1, "");
                if (result.Result == 1)
                {
                    var listData = JsonConvert.DeserializeObject<dynamic>(result.Body.ToString());
                    var data = listData.Rows[0];
                    DataTable dt = new DataTable();
                    dt.Columns.Add("enable");
                    dt.Columns.Add("guanlianzhenliao");
                    dt.Columns.Add("create_time");
                    dt.Columns.Add("caozuozhe");
                    dt.Columns.Add("Id");
                    dt.Columns.Add("IsDelete");
                    dt.Columns.Add("name");
                    dt.Columns.Add("sort");
                    dt.Columns.Add("code");
                    DataRow dr = dt.NewRow();
                    dr["enable"] = data.enable;
                    dr["guanlianzhenliao"] = data.guanlianzhenliao;
                    dr["create_time"] = data.caozuoshijian;
                    dr["caozuozhe"] = data.caozuozhe;
                    dr["Id"] = data.Id;
                    dr["IsDelete"] = data.IsDelete;
                    dr["name"] = data.name;
                    dr["sort"] = data.sort;
                    dr["code"] = data.code;
                    dt.Rows.Add(dr);
                    ViewBag.Data = dt;
                    return View();
                }
                return Content(result.Body.ToString());
            }
        }

        /// <summary>
        /// 保存新增或者编辑信息
        /// </summary>
        /// <returns></returns>
        public ActionResult EditSubmit()
        {
            zhenliaolujing zllj = new zhenliaolujing();
            zllj.caozuoshijian = string.IsNullOrEmpty(WebUtils.GetString("txtOptime")) ? null : (DateTime?)Convert.ToDateTime(WebUtils.GetString("txtOptime"));
            zllj.caozuozhe = Request["txtOpname"];
            zllj.code = Request["txtPathWayCode"];
            zllj.create_time = DateTime.Now;
            zllj.enable = false;
            zllj.guanlianzhenliao = Request["chkzhenduan"];
            zllj.Id = Convert.ToInt32(Request["txtId"]);
            zllj.IsDelete = 0;
            zllj.enable = Convert.ToInt32(Request["chkenabled"]) == 1;
            zllj.Id = Convert.ToInt32(Request["txtId"]);
            zllj.name = WebUtils.GetString("txtPathWayName");
            zllj.sort = WebUtils.GetInt("txtIsort");
            OperateResult resultInfo = ZhenLiaoLuJing.Setzhenliaolujing(JsonConvert.SerializeObject(zllj));
            return JsonDate(resultInfo);

        }

        public ActionResult GetPathNameList()
        {
            OperateResult resultInfo = PathwayManager.GetPathNameList();
            return JsonDate(resultInfo);
        }
    }
}
